

import java.util.*;
import java.io.*;

public class BotTrust {
	
	
	public static void main(String args[]){
		try{
			Scanner sc = new Scanner(new File("A-large.in"));
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("solve.in"))));
			int count = sc.nextInt();
			for(int i=1;i<=count;i++){
				int first = sc.nextInt();
				String al1 = "";
				ArrayList<Integer> al2 = new ArrayList<Integer>(first);
				for(int j=0;j<first;j++){
					al1=al1.concat(sc.next());
					al2.add(sc.nextInt());
				}
				int now = 0;
				int value =0;
				int bpos = 0;
				int opos = 0;
				boolean check = false;
				while(now<first){
					if(al1.charAt(now)=='O' && opos ==al2.get(now))check=true;
					else if(al1.charAt(now)=='B' && bpos ==al2.get(now))check=true;
					if(al1.indexOf("O", now) != -1){
						if(al2.get(al1.indexOf("O", now)) > opos)opos++;
						else if(al2.get(al1.indexOf("O", now)) < opos)opos--;
					}
					if(al1.indexOf("B", now) != -1){
						if(al2.get(al1.indexOf("B", now)) > bpos)bpos++;
						else if(al2.get(al1.indexOf("B", now)) < bpos)bpos--;
					}
	
					if(check==true){
						check = false;
						now++;
					}
					value++;
				}
				value--;
				pw.println("Case #"+i+": "+value);
			}
			pw.close();
		}catch(FileNotFoundException e){
			System.out.println(e);
		}catch(IOException e){
			System.out.println(e);			
		}
	}
	
}
